$(function() {
	
	$("#deleteLastFloor").click(function() {
		var floors = $(".floors");
		if ($(floors).size() < 2) {
			layer.msg("楼层数量至少要有一楼");
			return false;
		}else{
			var floorid = $(".floorIndex").last().val();
			deletedLastFloor(floorid);
			$(floors).last().remove();
			$("hr").last().remove();
			return true;
		}
		
	});

	
	  var $disabledResults = $(".goods"); $disabledResults.select2();
	  
	 var category = $(".category"); category.select2();
	 

});

function deletedLastFloor(floorid){
	$.ajax({
		url: "/manager/buildIndexPage/deletedFloor.do",
		data: {"floorNum":floorid},
		success: function(res){
			layer.msg(result.msg);
		}
	});
}

/*function addFloor(index) {
	var floor = $($("#floor").children()[0]).clone();
	// 得到最后一层楼的模板
	var floorIndex = "<span style='color:red;font-size:18px;'>" + index
			+ "楼</span>";
	$(floor).find("#floorsIndex").html(floorIndex);
	$(floor).find("input:hidden").val(index);
	var context = floor.html();
	
	$("#floor").append("<div class='floors'>" + context + "</div>").append(
			"<hr />");
}
*/
// 进入生成首页模板页面时首先查询是否已有楼层数据，若有数据，则加载数据
/*
function selectAllFloor() {
	$.ajax({
		url : "buildIndexPage/getFloorData.do",
		async : false,
		success : function(res) {
			//楼层循环
			for (var i = 0; i < res.length; i++) {
				var a = -1;
				// 每层楼的category循环
				for ( var item in res[i].category) {
					// 拼接分类
					++a;
					var categoryOptions = "<option value='" + item + "'>" + res[i].category[item] + "</option>";
					if (i == 0) {
						setCategoryData(a, categoryOptions);
					}
					var $cDiv = $(".cDiv")[0];
					var selectObj = $($cDiv).find("select")[a];
					console.log(categoryOptions);
					categoryOptions += $(selectObj).html();
					$(selectObj).html(categoryOptions);
				}

				var k = -1;
				// 每层楼的goods循环
				for ( var item in res[i].goods) {
					// 拼接商品
					++k;
					var goodsOptions = "<option value='" + item + "'>"
							+ res[i].goods[item] + "</options>";
					if (i == 0) {
						setGoodsData(k, goodsOptions);
					}
					var $gDiv = $(".gDiv")[0];
					var selectObj = $($gDiv).find("select")[k];
					goodsOptions += $(selectObj).html();
					$(selectObj).html(goodsOptions);
				}
				if(i < res.length - 1){
					addFloor(i + 2);
				}
			}
		}
	});
}

function setCategoryData(index, options) {

	$.ajax({
		url : "buildIndexPage/getCategoryData.do",
		async : false,
		data : {
			start : index
		},
		success : function(data) {
			var tempO = options;
			if (tempO == undefined || tempO == "") {
				tempO = "<option value='0'>请选择推荐分类</option>";
			}
			data.forEach(function(val, a) {
				tempO += "<option value='" + data[a].uuid + "'>" + data[a].name
						+ "</option>";
			});
			var currentNode = $(".category").eq(index);
			$(currentNode).append(tempO);
		}
	});
}

function setGoodsData(index, options) {
	$.ajax({
		url : "buildIndexPage/getGoodsData.do",
		async : false,
		data : {
			start : index
		},
		success : function(data) {
			var tempO = options;
			if (tempO == undefined || tempO == "") {
				tempO = "<option value='0'>请选择推荐商品</option>";
			}
			for (var i = 0; i < data.length; i++) {
				tempO += "<option value='" + data[i].uuid + "'>" + data[i].name
						+ "</option>";
			}
			var currentNode = $(".goods").eq(index);
			$(currentNode).append(tempO);
		}
	});
}
*/